package com.ebelter.myehc.ui.fragments.trends;


import android.graphics.Color;
import android.text.TextUtils;
import android.widget.LinearLayout;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.models.bluetooth.products.bracelet.bean.RateBean;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.GsonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.ThreadManager;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.btcomlib.views.CombinView8;
import com.ebelter.btcomlib.views.XLOneDayChartView;
import com.ebelter.myehc.R;
import com.ebelter.myehc.beans.OneDayChartViewDataBean;
import com.ebelter.myehc.models.http.response.device_resports.BRA_XL_R;
import com.ebelter.myehc.utils.EHCCommonUtils;

import java.util.List;

import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8SrcRes;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8Title;

/**
 * 趋势血压
 */

public class Trend_Bra_xinlv_Fragment extends BaseFragment {
    public static final String TAG = "Trend_Bra_xinlv_Fragment";
    //private static final int[] levelColors={0xff14e34e,0xffffcc00};


    CombinView7 bra_xl_cb7;
    CombinView8 bra_xl_cb8;
    LinearLayout ll_01;
    XLOneDayChartView xlOneDayChartView01;
    LinearLayout ll_02;
    XLOneDayChartView xlOneDayChartView02;
    LinearLayout ll_03;
    XLOneDayChartView xlOneDayChartView03;
    LinearLayout ll_04;
    XLOneDayChartView xlOneDayChartView04;
    LinearLayout ll_05;
    XLOneDayChartView xlOneDayChartView05;
    LinearLayout ll_06;
    XLOneDayChartView xlOneDayChartView06;
    LinearLayout ll_07;
    XLOneDayChartView xlOneDayChartView07;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_xinlv;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        bra_xl_cb7 = mRootView.findViewById(R.id.bra_xl_cb7);
        bra_xl_cb8 = mRootView.findViewById(R.id.bra_xl_cb8);


        if (AppUtils.en==AppUtils.getLocalStr()){
            bra_xl_cb7.setTitleSizes(13);
            bra_xl_cb8.setTitleSizes(14);
        }
        ll_01 = mRootView.findViewById(R.id.ll_01);

        int wC=Color.parseColor("#fdce02");
        int cC=getResources().getColor(R.color.redTheme);

        xlOneDayChartView01 = mRootView.findViewById(R.id.xLOneDayChartView01);
        xlOneDayChartView01.setColor1(wC);//警告线的颜色
        xlOneDayChartView01.setColor2(cC);//曲线的颜色
        xlOneDayChartView01.setYValue(40, 130);
        xlOneDayChartView01.setWarringValue(100);

        ll_02 = mRootView.findViewById(R.id.ll_02);
        xlOneDayChartView02 = mRootView.findViewById(R.id.xLOneDayChartView02);
        xlOneDayChartView02.setColor1(wC);//警告线的颜色
        xlOneDayChartView02.setColor2(cC);//曲线的颜色
        xlOneDayChartView02.setYValue(40, 130);
        xlOneDayChartView02.setWarringValue(100);


        ll_03 = mRootView.findViewById(R.id.ll_03);
        xlOneDayChartView03 = mRootView.findViewById(R.id.xLOneDayChartView03);
        xlOneDayChartView03.setColor1(wC);//警告线的颜色
        xlOneDayChartView03.setColor2(cC);//曲线的颜色
        xlOneDayChartView03.setYValue(40, 130);
        xlOneDayChartView03.setWarringValue(100);


        ll_04 = mRootView.findViewById(R.id.ll_04);
        xlOneDayChartView04 = mRootView.findViewById(R.id.xLOneDayChartView04);
        xlOneDayChartView04.setColor1(wC);//警告线的颜色
        xlOneDayChartView04.setColor2(cC);//曲线的颜色
        xlOneDayChartView04.setYValue(40, 130);
        xlOneDayChartView04.setWarringValue(100);



        ll_05 = mRootView.findViewById(R.id.ll_05);
        xlOneDayChartView05 = mRootView.findViewById(R.id.xLOneDayChartView05);
        xlOneDayChartView05.setColor1(wC);//警告线的颜色
        xlOneDayChartView05.setColor2(cC);//曲线的颜色
        xlOneDayChartView05.setYValue(40, 130);
        xlOneDayChartView05.setWarringValue(100);

        ll_06 = mRootView.findViewById(R.id.ll_06);
        xlOneDayChartView06 = mRootView.findViewById(R.id.xLOneDayChartView06);
        xlOneDayChartView06.setColor1(wC);//警告线的颜色
        xlOneDayChartView06.setColor2(cC);//曲线的颜色
        xlOneDayChartView06.setYValue(40, 130);
        xlOneDayChartView06.setWarringValue(100);

        ll_07 = mRootView.findViewById(R.id.ll_07);
        xlOneDayChartView07 = mRootView.findViewById(R.id.xLOneDayChartView07);
        xlOneDayChartView07.setColor1(wC);//警告线的颜色
        xlOneDayChartView07.setColor2(cC);//曲线的颜色
        xlOneDayChartView07.setYValue(40, 130);
        xlOneDayChartView07.setWarringValue(100);


        setDisOneDayChartViewCount(1);

    }




    private void setDisOneDayChartViewCount(int count){


        ViewUtils.goneView(ll_02);
        ViewUtils.goneView(ll_03);
        ViewUtils.goneView(ll_04);
        ViewUtils.goneView(ll_05);
        ViewUtils.goneView(ll_06);
        ViewUtils.goneView(ll_07);

        if (count>1){
            ViewUtils.displayView(ll_02);
        }
        if (count>2){
            ViewUtils.displayView(ll_03);
        }
        if (count>3){
            ViewUtils.displayView(ll_04);
        }
        if (count>4){
            ViewUtils.displayView(ll_05);
        }
        if (count>5){
            ViewUtils.displayView(ll_06);
        }
        if (count>6){
            ViewUtils.displayView(ll_07);
        }

    }


    public void setDisData(final BRA_XL_R baseResponse) {
        if (baseResponse == null || baseResponse.resultData == null) return;


        ThreadManager.getShortPool().execute(new Runnable() {
            @Override
            public void run() {



                final OneDayChartViewDataBean oneDayChartViewDataBean = new OneDayChartViewDataBean();//装载解析结果的

                if (baseResponse.resultData.summaryList != null && baseResponse.resultData.summaryList.size() > 0) {
                    oneDayChartViewDataBean.maxXl = baseResponse.resultData.summaryList.get(0).heartCountMax;
                    oneDayChartViewDataBean.minXl = baseResponse.resultData.summaryList.get(0).heartCountMin;
                    oneDayChartViewDataBean.avgXl = baseResponse.resultData.summaryList.get(0).heartCountAvg;
                }



                if (baseResponse.resultData.dateType == 1 || baseResponse.resultData.dateType == 2) {//--------------------年月
                    //不解析不处理
                } else if (baseResponse.resultData.dateType == 3||baseResponse.resultData.dateType == 4) {//--------------------周 和 日

                    List<BRA_XL_R.ResultDataBean.ChartListBean> chartListBeans = baseResponse.resultData.chartList;//拿到多少天的数据集合

                    if (chartListBeans != null) {//说明至少有一天是有数据的



                        for (int i = 0; i < chartListBeans.size(); i++) {

                            OneDayChartViewDataBean.OneDayChartViewDataBean_In in = new OneDayChartViewDataBean.OneDayChartViewDataBean_In();//用来装载一天的数据的

                            BRA_XL_R.ResultDataBean.ChartListBean bean = chartListBeans.get(i);//取出当天数据
                            String[] das = dealStr(bean.dataList);//把当天数据转换成一个String[]
                            long time1; //这个时间是记录当前天的数据的
                            if (das!= null){
                                time1=TimeUtils.parseFormatter3Time(bean.timeParam).getTime();//当天时间
                                for (String str:das){//说明当前项有数据
                                    BRA_XL_R.HeartBean heartBean = parseBean(str);
                                    if (heartBean!=null&&heartBean.heartcount>0){//说明当前项的数据是有效的
                                        long time2 = TimeUtils.parseFormatter1Time(heartBean.timeParam).getTime();//当前心跳时间
                                        time2=(time2-time1)/1000;//计算出当前心跳是今天的第几秒
                                        RateBean rateBean = new RateBean(time2,heartBean.heartcount);
                                        in.rateOneDayInfos.add(rateBean);
                                        oneDayChartViewDataBean.newestXl=heartBean.heartcount;
                                    }
                                }
                                in.setDrawButtomTitle(true,"00:00",TimeUtils.formatTime5(time1),"24:00");

                            }

                            in.calculateStartEndY();

                            oneDayChartViewDataBean.list.add(in);

                        }



                        post(new Runnable() {
                            @Override
                            public void run() {


                                int size= oneDayChartViewDataBean.list.size();
                                setDisOneDayChartViewCount(size);//设置显示的表格数量

                                if (size==0){
                                    //说明一条数据都没有
                                    setxlOneDayChartView01Data(getXlOneDayChartView(0),null);
                                }else {
                                    for (int i=0;i<size;i++){
                                        setxlOneDayChartView01Data(getXlOneDayChartView(i),oneDayChartViewDataBean.list.get(i));
                                    }
                                }



                                setCb8Title(bra_xl_cb8, 2, NumUtils.disIntZero(oneDayChartViewDataBean.minXl,N2));
                                setCb8Title(bra_xl_cb8, 3, NumUtils.disIntZero(oneDayChartViewDataBean.maxXl,N2));
                                setCb8Title(bra_xl_cb8, 4, NumUtils.disIntZero(oneDayChartViewDataBean.avgXl,N2));
                                setCb8SrcRes(bra_xl_cb8,EHCCommonUtils.getDisplaySrcRes(oneDayChartViewDataBean.avgXl,oneDayChartViewDataBean.newestXl));


                            }
                        });



                    }


                }




            }
        });

    }

    private XLOneDayChartView getXlOneDayChartView(int i){
        if (i==0){
            return xlOneDayChartView01;
        }
        if (i==1){
            return xlOneDayChartView02;
        }
        if (i==2){
            return xlOneDayChartView03;
        }

        if (i==3){
            return xlOneDayChartView04;
        }

        if (i==4){
            return xlOneDayChartView05;
        }
        if (i==5){
            return xlOneDayChartView06;
        }

        if (i==6){
            return xlOneDayChartView07;
        }


        return null;

    }

    private void setxlOneDayChartView01Data(XLOneDayChartView xl,OneDayChartViewDataBean.OneDayChartViewDataBean_In in){
        if (xl!=null){
            if (in!=null){
                xl.setYValue(in.startY,in.endY);
                xl.setButtomTitle(in.isDrawButtomTitle,in.t1,in.t2,in.t3);
                xl.setRateOneDayInfos(in.rateOneDayInfos);
            }else {
                xl.setYValue(0,1);
                xl.setButtomTitle(true,"00:00","","24:00");
                xl.setRateOneDayInfos(null);
            }

        }
    }


    private String[] dealStr(String data) {
        if (!TextUtils.isEmpty(data)) {
            data = data.replace("[", "").replace("]", "").replace("\\", "");
            data = data.replace("},{", "}#{");
            String[] das = data.split("#");
            return das;
        }
        return null;
    }

    private BRA_XL_R.HeartBean parseBean(String json) {
        if (!TextUtils.isEmpty(json) && json.startsWith("{") && json.endsWith("}")) {
            return GsonUtils.getInstance().fromJson(json, BRA_XL_R.HeartBean.class);
        }
        return null;
    }


}
